package com.erp.statistics.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.erp.statistics.order.mapper.ErpChildOrderStatisticsMapper;
import com.erp.statistics.order.service.IErpChildOrderStatisticsService;
import com.ruoyi.common.core.web.oldDomain.BaseEntity;
import com.ruoyi.statistics.api.domain.ErpChildOrderStatistics;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 子订单信息统计Service业务层处理
 *
 * @author erp
 * @date 2023-07-19
 */
@Service
public class ErpChildOrderStatisticsServiceImpl implements IErpChildOrderStatisticsService {

    @Resource
    private ErpChildOrderStatisticsMapper baseMapper;
//    private final ErpOrderInfoStatisticsMapper orderInfoStatisticsMapper;
//    private final ErpSellerOrderMapper sellerOrderMapper;
//


    /**
     * 根据子订单号，查询子订单统计信息
     *
     * @param childOrderId
     * @return
     */
    public ErpChildOrderStatistics queryByChildOrderId(String childOrderId) {
        return baseMapper.selectOne(
                new LambdaQueryWrapper<ErpChildOrderStatistics>()
                        .eq(ErpChildOrderStatistics::getChildOrderId, childOrderId));
    }

    /**
     * 根据条件查询数据列表
     * @param storeId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<ErpChildOrderStatistics> selectChildOrderStatisticsList(Long storeId, Date startTime, Date endTime) {
        return baseMapper.selectList(new LambdaQueryWrapper<ErpChildOrderStatistics>()
                .eq(ErpChildOrderStatistics::getStoreId, storeId)
                .ge(BaseEntity::getCreateTime, startTime)
                .le(BaseEntity::getCreateTime, endTime)
                .like(ErpChildOrderStatistics::getProductProfit, "-")
                .groupBy(ErpChildOrderStatistics::getCollectProductId)
                .last("order by CAST(product_profit AS DOUBLE) asc"));
    }
//    /**
//     * 查询子订单信息统计
//     */
//    @Override
//    public ErpChildOrderStatisticsVo queryById(Long id){
//        return baseMapper.selectVoById(id);
//    }
//
//    /**
//     * 查询子订单信息统计列表
//     */
//    @Override
//    public TableDataInfo<ErpChildOrderStatisticsVo> queryPageList(ErpChildOrderStatisticsBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<ErpChildOrderStatistics> lqw = buildQueryWrapper(bo);
//        Page<ErpChildOrderStatisticsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
//        return TableDataInfo.build(result);
//    }
//
//    /**
//     * 查询子订单信息统计列表
//     */
//    @Override
//    public List<ErpChildOrderStatisticsVo> queryList(ErpChildOrderStatisticsBo bo) {
//        LambdaQueryWrapper<ErpChildOrderStatistics> lqw = buildQueryWrapper(bo);
//        return baseMapper.selectVoList(lqw);
//    }
//
//    private LambdaQueryWrapper<ErpChildOrderStatistics> buildQueryWrapper(ErpChildOrderStatisticsBo bo) {
//        Map<String, Object> params = bo.getParams();
//        LambdaQueryWrapper<ErpChildOrderStatistics> lqw = Wrappers.lambdaQuery();
//        lqw.eq(bo.getOrderId() != null, ErpChildOrderStatistics::getOrderId, bo.getOrderId());
//        lqw.eq(StringUtils.isNotBlank(bo.getChildOrderId()), ErpChildOrderStatistics::getChildOrderId, bo.getChildOrderId());
//        lqw.eq(StringUtils.isNotBlank(bo.getSkuCode()), ErpChildOrderStatistics::getSkuCode, bo.getSkuCode());
//        lqw.eq(StringUtils.isNotBlank(bo.getProductCount()), ErpChildOrderStatistics::getProductCount, bo.getProductCount());
//        lqw.eq(StringUtils.isNotBlank(bo.getProductUnitPrice()), ErpChildOrderStatistics::getProductUnitPrice, bo.getProductUnitPrice());
//        lqw.eq(StringUtils.isNotBlank(bo.getProductTotalPrice()), ErpChildOrderStatistics::getProductTotalPrice, bo.getProductTotalPrice());
//        lqw.eq(StringUtils.isNotBlank(bo.getLogisticsUnitPrice()), ErpChildOrderStatistics::getLogisticsUnitPrice, bo.getLogisticsUnitPrice());
//        lqw.eq(StringUtils.isNotBlank(bo.getLogisticsTotalPrice()), ErpChildOrderStatistics::getLogisticsTotalPrice, bo.getLogisticsTotalPrice());
//        lqw.eq(StringUtils.isNotBlank(bo.getCostUnitPrice()), ErpChildOrderStatistics::getCostUnitPrice, bo.getCostUnitPrice());
//        lqw.eq(StringUtils.isNotBlank(bo.getCostTotalPrice()), ErpChildOrderStatistics::getCostTotalPrice, bo.getCostTotalPrice());
//        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpChildOrderStatistics::getRevision, bo.getRevision());
//        return lqw;
//    }
//
//    /**
//     * 新增子订单信息统计
//     */
//    @Override
//    public Boolean insertByBo(ErpChildOrderStatisticsBo bo) {
//        ErpChildOrderStatistics add = BeanUtil.toBean(bo, ErpChildOrderStatistics.class);
//        validEntityBeforeSave(add);
//        boolean flag = baseMapper.insert(add) > 0;
//        if (flag) {
//            bo.setId(add.getId());
//        }
//        return flag;
//    }
//
//    /**
//     * 修改子订单信息统计
//     */
//    @Override
//    public Boolean updateByBo(ErpChildOrderStatisticsBo bo) {
//        ErpChildOrderStatistics update = BeanUtil.toBean(bo, ErpChildOrderStatistics.class);
//        validEntityBeforeSave(update);
//        return baseMapper.updateById(update) > 0;
//    }
//
//    /**
//     * 保存前的数据校验
//     */
//    private void validEntityBeforeSave(ErpChildOrderStatistics entity){
//        //TODO 做一些数据校验,如唯一约束
//    }
//
//    /**
//     * 批量删除子订单信息统计
//     */
//    @Override
//    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
//        if(isValid){
//            //TODO 做一些业务上的校验,判断是否需要校验
//        }
//        return baseMapper.deleteBatchIds(ids) > 0;
//    }
//
//    @Override
//    public TableDataInfo<ErpChildOrderStatisticsVo> getStaticsByCode(ErpChildOrderStatisticsBo bo,PageQuery pageQuery) {
//        Page<ErpChildOrderStatisticsVo> staticsByCode = baseMapper.getStaticsByCode(pageQuery.build(),bo);
////        StatisticSummaryDataVo summary = baseMapper.getSummaryData(staticsByCode.getRecords());
////        Map<String, Object> map = new HashMap<>();
////        map.put("list", staticsByCode);
////        map.put("summary",summary);
//        return TableDataInfo.build(staticsByCode);
//    }
}
